<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/10/20
 * Time: 20:28
 */
header("content-type:text/html;charset=utf-8");
require COMMON_PATH.'logTool.class.php';
require COMMON_PATH.'message.class.php';
class GoodAction extends Action
{
    //套餐首页
    public function index(){
        $user = isset($_SESSION['user'])?$_SESSION['user']:'';
        logTool::logCat('session: '.$user);
        if (empty($user)){
            $this->error("请先登录",U("index/login"));
        }
        $good = M('good');
        $where = array('delete' => 1);
        $data = $good->where($where)->select();
        $good_class = M('good_class');
        $class = $good_class->where(array('status' => 1))->select();
        foreach ($data as $item => $value) {
            $id = $value['class'];
            foreach ($class as $k => $v) {
                if ($id == $v['id'])
                    $data[$item]['class_name'] = $v["class"];
            }
        }
        $this->assign('list', $data);
        $this->display();
    }

    //查询所有套餐类型
    public function getAllGoodClass(){
        $back = message::fail('查询套餐类型失败');
        $good_class = M('good_class');
        $data = $good_class->where('status = 1')->select();
        if ($data){
            $back = message::success('查询成功',$data);
        }
        logTool::logCat('查询所有套餐类型返回信息: '.$back);
        echo $back;
        return;
    }

    //查询所有套餐
    public function getAllGood(){
        $back = message::fail('查询套餐失败');
        $good = M('good');
        $where = array('delete' => 1);
        $data = $good->where($where)->select();
        $good_class = M('good_class');
        $class = $good_class->where(array('status' => 1))->select();
        if ($data){
            foreach ($data as $item=>$value){
                $id = $value['class'];
                foreach ($class as $k=>$v){
                    if ($id == $v['id'])
                        $data[$item]['class_name'] = $v["class"];
                }
            }
            $this->assign('list',$data);
            $content = $this->fetch();
            $back = message::success('查询成功',$content);
        }
        logTool::logCat('查询所有套餐返回信息: '.$back);
        echo $back;
        return;
    }

    //预添加套餐
    public function preAddGood(){
        $back = message::fail('');
        $content = $this->fetch();
        $back = message::success('',$content);
        logTool::logCat('预添加套餐返回信息: '.$back);
        echo $back;
    }

    //添加套餐
    public function addGood(){
        $back = message::fail('添加套餐失败');
        $good = M('good');
        //接收参数
        $class = isset($_REQUEST['class'])?$_REQUEST['class']:'';
        $name = isset($_REQUEST['name'])?$_REQUEST['name'].trim():'';
        $price = isset($_REQUEST['price'])?$_REQUEST['price'].trim():'';
        $nice1 = isset($_REQUEST['nice1'])?$_REQUEST['nice1'].trim():'';
        $nice2 = isset($_REQUEST['nice2'])?$_REQUEST['nice2'].trim():'';
        $status = isset($_REQUEST['status'])?$_REQUEST['status']:'';
        //套餐名称、费用不可为空
        if (empty($name) || empty($price)){
            $back = message::fail('套餐名称和价格必须填写');
            echo $back;
            return;
        }
        $data = array(
            'class' => $class,
            'name' => $name,
            'price' => $price,
            'nice1' => $nice1,
            'nice2' => $nice2,
            'status' => $status
        );
        if ($good->add($data)){
            $back = message::success('添加套餐成功');
        }
        logTool::logCat('添加套餐返回信息: '.$back);
        echo $back;
        return;
    }

    //查询单个套餐信息
    public function getSingleGood($goodId){
        $back = message::fail('查询单个套餐失败');
        $good = M('good');
        $condition = array(
            'id' => $goodId,
            'delete' => 1
        );
        $data = $good->where($condition)->find();
        logTool::logCat($good->getLastSql());
        if ($data){
            foreach ($data as $key=>$vle){
                logTool::logCat('编码格式：'.$vle.'------>'.mb_detect_encoding($vle,array("ASCII","UTF-8","GB2312","GBK","BIG5")));
            }
            $this->assign('singleGood_list',$data);
            $content = $this->fetch();
            logTool::logCat("返回网页内容=====>".$content);
            $back = message::success('返回成功',$content);
        }
        logTool::logCat('查询单个套餐返回信息：'.$back);
        echo $back;
        return;
    }

    //保存套餐编辑信息
    public function saveGood(){
        $good = M('good');
        //接收参数
        $id = isset($_REQUEST['id'])?$_REQUEST['id']:'';
        $class = isset($_REQUEST['class'])?$_REQUEST['class']:'';
        $name = isset($_REQUEST['name'])?$_REQUEST['name'].trim():'';
        $price = isset($_REQUEST['price'])?$_REQUEST['price'].trim():'';
        $nice1 = isset($_REQUEST['nice1'])?$_REQUEST['nice1'].trim():'';
        $nice2 = isset($_REQUEST['nice2'])?$_REQUEST['nice2'].trim():'';
        $status = isset($_REQUEST['status'])?$_REQUEST['status']:'';
        logTool::logCat('step 1');
        //套餐名称、费用不可为空
        if (empty($name) || empty($price)){
            logTool::logCat('step 2');
            $this->error('填写格式错误');
        } else {
            $data = array(
                'class' => $class,
                'name' => $name,
                'price' => $price,
                'nice1' => $nice1,
                'nice2' => $nice2,
                'status' => $status
            );
            $condition = array(
                'id' => $id
            );
            if ($good->where($condition)->save($data)) {
                logTool::logCat('step 3');
                logTool::logCat($good->getLastSql());
                $this->success("保存成功", U('good/index'));
            } else {
                $this->error('保存失败');
            }
        }
    }

    //删除套餐
    public function deleteGood($goodId){
        $back = message::fail('删除套餐失败');
        $good = M('good');
        if ($good->where("id = {$goodId}")->save(array('delete' => 0))){
            $back = message::success('删除套餐成功');
        }
        logTool::logCat('删除套餐返回信息: '.$back);
        echo $back;
        return;
    }
}